package com.macro.mall.product.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.product.constant.Constants;
import com.macro.mall.product.dto.*;
import com.macro.mall.product.service.PmProdSpecService;
import com.macro.mall.product.service.PmProdSpecSkuService;
import com.macro.mall.product.service.PmPrptyPackageService;
import com.macro.mall.product.util.DozerUtils;
import com.macro.mall.product.util.LocationUtils;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class PmProdSpecServiceImpl implements PmProdSpecService {
    @Autowired
    private PmProdSpecMapper pmProdSpecMapper;
    @Autowired
    private PmProdSpecPicMapper pmProdSpecPicMapper;
    @Autowired
    private PmPrptyPackageService prptyPackageService;
    @Autowired
    private PmSpecSkuMapper pmSpecSkuMapper;
    @Autowired
    private PmProdSpecSkuService pmProdSpecSkuService;
    @Autowired
    private PmSpecPricePlanPrptyRelMapper pmSpecPricePlanPrptyRelMapper;
    @Autowired
    private Mapper mapper;
    @Autowired
    private PmProdSpecStockTempMapper pmProdSpecStockTempMapper;
    @Autowired
    private CommonMapper commonMapper;
    @Autowired
    private PmProdSkuInstMapper pmProdSkuInstMapper;
    @Autowired
    private PmProdPricePlanPrptyRelMapper pmProdPricePlanPrptyRelMapper;
    @Autowired
    private PmProdPrptyValMapper pmProdPrptyValMapper;
    @Autowired
    private SqlCommonMapper sqlCommonMapper;
    @Autowired
    private PmProdSpecPrptyMapper pmProdSpecPrptyMapper;
    @Autowired
    private PmProdSpecPrptyValMapper pmProdSpecPrptyValMapper;
    @Autowired
    private PmProdSpecDetailMapper pmProdSpecDetailMapper;

    @Autowired
    private PmServerSpecMapper pmServerSpecMapper;

    @Override
    public List<PmProdSpec> list(PmProdSpecDto param) {

        PmProdSpec record = new PmProdSpec();
        if (!StringUtils.isBlank(param.getSpecName())) {
            record.setSpecName(param.getSpecName());
        }
        if (param.getServerSpecId() != null) {
            record.setServerSpecId(param.getServerSpecId());
        }
        List<PmProdSpec> prodSpecList = pmProdSpecMapper.selectList(record);
        return prodSpecList;
    }

    @Override
    public List<PmProdSpec> listPage(int pageNum, int pageSize, PmProdSpecDto param) {
        PageHelper.startPage(pageNum, pageSize);
        List<PmProdSpec> prodSpecList = this.list(param);
        return prodSpecList;
    }

    @Override
    public int save(PmProdSpecSaveDto prodSpecDto) {
        int count = 0;
        if (prodSpecDto != null) {
            /**1.数据转换**********************/
            PmProdSpec prodSpec = mapper.map(prodSpecDto, PmProdSpec.class);
            Date curDate = new Date();

            /**2.基础数据保存******************/
            if (prodSpec.getId() != null && prodSpec.getId() > 0) {

                // prodSpec.setUpdateBy("xx");
                prodSpec.setUpdateTime(curDate);
                count = pmProdSpecMapper.updateByPrimaryKeySelective(prodSpec);
            } else {
                // prodSpec.setUpdateBy("xx");
                prodSpec.setUpdateTime(curDate);
                //   prodSpec.setCreateBy("xx");
                prodSpec.setCreateTime(curDate);
                prodSpec.setStatus(Constants.STATUS_VALID);
                count = pmProdSpecMapper.insert(prodSpec);
            }
            if(StringUtils.isBlank(prodSpec.getSpecCode())){
                PmProdSpec specTemp = new PmProdSpec();
                specTemp.setId(prodSpec.getId());
                specTemp.setSpecCode(prodSpec.getId().toString());
                pmProdSpecMapper.updateByPrimaryKeySelective(specTemp);
                prodSpec.setSpecCode(prodSpec.getId().toString());
            }
            prodSpecDto.setId(prodSpec.getId());
            /**3.图片保存******************/
            savePic(prodSpecDto);
            /**4.Sku保存******************/
            saveSku(prodSpecDto);
            /**5.库存模板*****************/
            saveStockTemp(prodSpecDto);
            /**6.属性保存*****************/
            savePrpty(prodSpecDto);
            /**7.详情*****************/
            saveSpecDetail(prodSpecDto);
        }
        return count;
    }

    @Override
    public int delete(String prodSpecIds) {
        int count = 0;
        if (!StringUtils.isBlank(prodSpecIds)) {
            String[] prodSpecIdArray = prodSpecIds.split(";");
            PmProdSpec pmProdSpec;
            Date curDate = new Date();
            for (String prodSpecId : prodSpecIdArray) {
                pmProdSpec = new PmProdSpec();
                pmProdSpec.setId(Long.valueOf(prodSpecId));
                pmProdSpec.setStatus(Constants.STATUS_INVALID);
                pmProdSpec.setUpdateTime(curDate);
                // pmProdSpec.setUpdateBy("xx");
                int countInfo = pmProdSpecMapper.updateByPrimaryKeySelective(pmProdSpec);
                count = count + countInfo;
            }
        }
        return count;
    }

    private List<PmProdSpecPic> getPicList(PmProdSpecPic picParam) {
        PmProdSpecPicExample example = new PmProdSpecPicExample();
        PmProdSpecPicExample.Criteria criteria = example.createCriteria();
        if (picParam.getProdSpecId() != null) {
            criteria.andProdSpecIdEqualTo(picParam.getProdSpecId());
        }
        example.setOrderByClause(" pic_type asc ,pic_seq asc ");
        List<PmProdSpecPic> picList = pmProdSpecPicMapper.selectByExample(example);
        return picList;
    }
    private List<PmProdSpecStockTemp> getProdSpecStockTempList(PmProdSpecStockTemp prodSpecStockTemp) {
        PmProdSpecStockTempExample example = new PmProdSpecStockTempExample();
        PmProdSpecStockTempExample.Criteria criteria = example.createCriteria();
        criteria.andProdSpecIdEqualTo(prodSpecStockTemp.getProdSpecId());
        example.setOrderByClause(" temp_seq asc ");
        List<PmProdSpecStockTemp> tempList = pmProdSpecStockTempMapper.selectByExample(example);
        return tempList;
    }

    private List<PmProdSpecPrpty> getProdSpecPrptyList(PmProdSpecPrpty prodSpecPrpty) {
        PmProdSpecPrptyExample prptyExample = new PmProdSpecPrptyExample();
        PmProdSpecPrptyExample.Criteria prptyCriteria = prptyExample.createCriteria();
        prptyCriteria.andProdSpecIdEqualTo(prodSpecPrpty.getProdSpecId());
        prptyCriteria.andStatusEqualTo(Constants.STATUS_VALID);
        prptyExample.setOrderByClause(" prpty_seq asc ");
        List<PmProdSpecPrpty> prodSpecPrptyList = pmProdSpecPrptyMapper.selectByExample(prptyExample);
        if (prodSpecPrptyList != null && prodSpecPrptyList.size() > 0) {
            List<PmProdSpecPrptyVal> prodSpecPrptyValList;
            PmProdSpecPrptyValExample prptyValExample;
            PmProdSpecPrptyValExample.Criteria prptyValCriteria;
            for (PmProdSpecPrpty prodSpecPrptyTemp : prodSpecPrptyList) {
                prptyValExample = new PmProdSpecPrptyValExample();
                prptyValCriteria = prptyValExample.createCriteria();
                prptyValCriteria.andSpecPrptyIdEqualTo(prodSpecPrptyTemp.getId());
                prptyValCriteria.andStatusEqualTo(Constants.STATUS_VALID);
                prptyValExample.setOrderByClause(" prpty_seq asc ");
                prodSpecPrptyValList = pmProdSpecPrptyValMapper.selectByExample(prptyValExample);
                prodSpecPrptyTemp.setProdSpecPrptyValList(prodSpecPrptyValList);
            }
        }
        return prodSpecPrptyList;
    }

    private void savePrpty(PmProdSpecSaveDto pmProdSpecDto) {
        //1 删除原有属性、属性值
        PmProdSpecPrptyExample prptyExample = new PmProdSpecPrptyExample();
        PmProdSpecPrptyExample.Criteria prptyCriteria = prptyExample.createCriteria();
        prptyCriteria.andProdSpecIdEqualTo(pmProdSpecDto.getId());
        prptyCriteria.andStatusEqualTo(Constants.STATUS_VALID);
        pmProdSpecPrptyMapper.deleteByExample(prptyExample);

        PmProdSpecPrptyValExample prptyValExample = new PmProdSpecPrptyValExample();
        PmProdSpecPrptyValExample.Criteria prptyValCriteria = prptyValExample.createCriteria();
        prptyValCriteria.andProdSpecIdEqualTo(pmProdSpecDto.getId());
        prptyValCriteria.andStatusEqualTo(Constants.STATUS_VALID);
        pmProdSpecPrptyValMapper.deleteByExample(prptyValExample);

        //2 构造属性、属性值数据
        List<PmProdSpecPrptyDto> prodSpecPrptyList = pmProdSpecDto.getProdSpecPrptyList();
        if(prodSpecPrptyList!=null && prodSpecPrptyList.size()>0){
            PmProdSpecPrptyDto prptyDto ;
            PmProdSpecPrpty  prptyDB;
            PmProdSpecPrptyValDto prptyValDto ;
            PmProdSpecPrptyVal  prptyValDB;
            for(int i= 0 ; i <prodSpecPrptyList.size();i++){
                prptyDto = prodSpecPrptyList.get(i);
                if(prptyDto!=null && prptyDto.getProdSpecPrptyValList().size()>0){
                    prptyDB = new PmProdSpecPrpty();
                    prptyDB.setProdSpecId(pmProdSpecDto.getId());
                    prptyDB.setPrptyCode(prptyDto.getPrptyCode());
                    prptyDB.setPrptyType(prptyDto.getPrptyType());
                    prptyDB.setPrptyName(prptyDto.getPrptyName());
                    prptyDB.setPrptyNameShow(prptyDto.getPrptyNameShow());
                    prptyDB.setPrptyDesc(prptyDto.getPrptyDesc());
                    prptyDB.setCategory(prptyDto.getCategory());
                    prptyDB.setPrptySeq(i);
                    prptyDB.setStatus(Constants.STATUS_VALID);
                    pmProdSpecPrptyMapper.insert(prptyDB);

                    for(int j =0 ; j<prptyDto.getProdSpecPrptyValList().size();j++){
                        prptyValDto = prptyDto.getProdSpecPrptyValList().get(j);
                        prptyValDB = new PmProdSpecPrptyVal();
                        prptyValDB.setProdSpecId(pmProdSpecDto.getId());
                        prptyValDB.setPrptyCode(prptyValDto.getPrptyCode());
                        prptyValDB.setPrptyValCode(prptyValDto.getPrptyValCode());
                        prptyValDB.setPrptyValue(prptyValDto.getPrptyValue());
                        prptyValDB.setStatus(Constants.STATUS_VALID);
                        prptyValDB.setPrptySeq(j);
                        prptyValDB.setSpecPrptyId(prptyDB.getId());
                        pmProdSpecPrptyValMapper.insert(prptyValDB);
                    }
                }
            }
        }
    }

    private void saveSpecDetail(PmProdSpecSaveDto pmProdSpecDto) {
        //1 删除原有数据
        PmProdSpecDetailExample example = new PmProdSpecDetailExample();
        PmProdSpecDetailExample.Criteria criteria = example.createCriteria();
        criteria.andProdSpecIdEqualTo(pmProdSpecDto.getId());
        pmProdSpecDetailMapper.deleteByExample(example);

        if(pmProdSpecDto.getPmProdSpecDetail()!=null && !StringUtils.isBlank(pmProdSpecDto.getPmProdSpecDetail().getProdSpecDetail())){
            PmProdSpecDetail detail = new PmProdSpecDetail();
            detail.setProdSpecId(pmProdSpecDto.getId());
            detail.setProdSpecDetail(pmProdSpecDto.getPmProdSpecDetail().getProdSpecDetail());
            pmProdSpecDetailMapper.insert(detail);
        }
    }

    private  void savePic(PmProdSpecSaveDto pmProdSpecDto){
        //1 删除原有图片
        PmProdSpecPicExample example = new PmProdSpecPicExample();
        PmProdSpecPicExample.Criteria criteria = example.createCriteria();
        criteria.andProdSpecIdEqualTo(pmProdSpecDto.getId());
        pmProdSpecPicMapper.deleteByExample(example);
        //2 构造图片数据
        List<PmProdSpecPic> picList = new ArrayList<PmProdSpecPic>();
        if (!StringUtils.isBlank(pmProdSpecDto.getMainPicId())) {
            int seq = 0;
            String[] picArray = pmProdSpecDto.getMainPicId().split(";");
            PmProdSpecPic pic;
            for (String picId : picArray) {
                pic = new PmProdSpecPic();
                pic.setProdSpecId(pmProdSpecDto.getId());
                pic.setPicType(Constants.PROD_SPEC_PIC_TYPE_MAIN);
                seq++;
                pic.setPicSeq(seq);
                pic.setPicId(picId);
                picList.add(pic);
            }
        }
        //3 保存图片
        if (picList != null && picList.size() > 0) {
            for (PmProdSpecPic pic : picList) {
                pmProdSpecPicMapper.insert(pic);
            }
        }
    }
    private void saveSku(PmProdSpecSaveDto pmProdSpecDto) {
        /**1.获取数据库数据******************/
        PmSpecSku pmSpecSku = new PmSpecSku();
        pmSpecSku.setProdSpecId(pmProdSpecDto.getId());
        pmSpecSku.setStatus(String.valueOf(Constants.STATUS_VALID));
        List<PmSpecSku> specSkuDBList = pmProdSpecSkuService.getSpecSkuList(pmSpecSku);

        /**2.封装修改、删除、新增集合******/
        List<Long> delSkuList = new ArrayList<Long>();
        List<PmSpecSku> updateSkuList = new ArrayList<PmSpecSku>();
        JSONArray addArray = new JSONArray();
        if (pmProdSpecDto.getSkuTableData() == null || pmProdSpecDto.getSkuTableData().size() == 0) {
            //全部删除，
            if (specSkuDBList != null && specSkuDBList.size() > 0) {
                for (PmSpecSku skuDB : specSkuDBList) {
                    delSkuList.add(skuDB.getId());
                }
            }
        } else if (specSkuDBList == null || specSkuDBList.size() == 0) {
            //全部新增
            if (pmProdSpecDto.getSkuTableData() != null || pmProdSpecDto.getSkuTableData().size() > 0) {
                addArray=pmProdSpecDto.getSkuTableData();
            }
        } else {
            JSONObject skuPage;
            String mainId;
            String price;
            String[] dataArray;
            PmSpecSku upSpecSku;
            boolean isFind;

            //修改、删除结果集构建
            for (PmSpecSku skuDB : specSkuDBList) {
                isFind = false;
                for (int j = 0; j < pmProdSpecDto.getSkuTableData().size(); j++) {
                    skuPage = pmProdSpecDto.getSkuTableData().getJSONObject(j);

                    mainId = skuPage.getString("mainId");
                    String keyDB = skuDB.getId() + "," + skuDB.getCode();
                    if (!StringUtils.isBlank(mainId)) {
                        dataArray = mainId.split("\\|");
                        if (dataArray.length > 1 && dataArray[1].equals(keyDB)) {
                            upSpecSku = new PmSpecSku();
                            upSpecSku.setId(skuDB.getId());
                            upSpecSku.setPrice(skuPage.getFloat("price"));
                            updateSkuList.add(upSpecSku);
                            isFind = true;
                            break;
                        }
                    }
                }
                if (!isFind) {
                    delSkuList.add(skuDB.getId());
                }
            }
            //新增结果集构建
            for (int j = 0; j < pmProdSpecDto.getSkuTableData().size(); j++) {
                skuPage = pmProdSpecDto.getSkuTableData().getJSONObject(j);
                mainId = skuPage.getString("mainId");
                if (!StringUtils.isBlank(mainId)) {
                    dataArray = mainId.split("\\|");
                    if (dataArray.length == 1) {
                        addArray.add(skuPage);
                    }
                }
            }
        }
        /**3.数据库保存*************/
        if (delSkuList != null && delSkuList.size() > 0) {
            PmSpecPricePlanPrptyRel prptyRel;
            for (Long skuId : delSkuList) {
                prptyRel = new PmSpecPricePlanPrptyRel();
                prptyRel.setStatus(Constants.STATUS_INVALID);
                prptyRel.setSkuId(skuId);
                pmSpecPricePlanPrptyRelMapper.updateStatusBySkuId(prptyRel);

                PmSpecSku upSpecSku = new PmSpecSku();
                upSpecSku.setId(skuId);
                upSpecSku.setStatus("1");
                pmSpecSkuMapper.updateByPrimaryKeySelective(upSpecSku);
            }
        }
        if (updateSkuList != null && updateSkuList.size() > 0) {
            for (PmSpecSku upSku : updateSkuList) {
                pmSpecSkuMapper.updateByPrimaryKeySelective(upSku);
            }
        }
        if(addArray!=null && addArray.size()>0){
            for(int i=0 ;i<addArray.size(); i++){
                JSONObject object = addArray.getJSONObject(i);
                PmSpecSku addSku = new PmSpecSku();
                addSku.setProdSpecId(pmProdSpecDto.getId());
                if(object.get("price")!=null) {
                    addSku.setPrice(object.getFloat("price"));
                }
                addSku.setStatus("0");
                addSku.setSeq(i);
                pmSpecSkuMapper.insert(addSku);
                if(StringUtils.isBlank(addSku.getCode())){
                    PmSpecSku skuTemp = new PmSpecSku();
                    skuTemp.setId(addSku.getId());
                    skuTemp.setCode(addSku.getId().toString());
                    skuTemp.setPrice(addSku.getPrice());
                    pmSpecSkuMapper.updateByPrimaryKeySelective(skuTemp);
                    addSku.setCode(addSku.getId().toString());
                }
                //增加sku属性值
                String mainId=object.getString("mainId");
                PmSpecPricePlanPrptyRel prptyRel ;
                if(!StringUtils.isBlank(mainId)){
                    String[] prptyValArray = mainId.split(";");
                    String prptyValInfo;
                    for(int save_i =0 ; save_i<prptyValArray.length;save_i++){
                        prptyValInfo = prptyValArray[save_i];
                        String[] prptyValInfoArray = prptyValInfo.split(",");
                        prptyRel = new PmSpecPricePlanPrptyRel();
                        prptyRel.setProdSpecId(addSku.getProdSpecId());
                        prptyRel.setPrptyCode(prptyValInfoArray[0]);
                        prptyRel.setPrptyValCode(prptyValInfoArray[1]);
                        prptyRel.setPrptyNameShow(object.getString("prptyNameShow_"+save_i));
                        prptyRel.setPrptyValue(object.getString("prpty_"+save_i));
                        prptyRel.setStatus(Constants.STATUS_VALID);
                        prptyRel.setSeq(1);
                        prptyRel.setSkuId(addSku.getId());
                        pmSpecPricePlanPrptyRelMapper.insert(prptyRel);
                    }
                }

            }
        }
    }

    @Override
    public PmProdSpec getById(Long id) {
        PmProdSpec pmProdSpec = pmProdSpecMapper.selectByPrimaryKey(id);
        PmServerSpec  pmServerSpec = pmServerSpecMapper.selectByPrimaryKey(pmProdSpec.getServerSpecId());
        if(pmServerSpec!=null){
            pmProdSpec.setServerSpecName(pmServerSpec.getServerSpecName());
        }
        //图片信息
        PmProdSpecPic pic = new PmProdSpecPic();
        pic.setProdSpecId(pmProdSpec.getId());
        pmProdSpec.setPicList(getPicList(pic));

        //品类包信息
        PmPrptyPackage prptyPackage = prptyPackageService.getById(pmProdSpec.getPrptyPackageId());
        pmProdSpec.setPrprtyPackage(prptyPackage);

        //SKU信息
        PmSpecSku pmSpecSku = new PmSpecSku();
        pmSpecSku.setProdSpecId(pmProdSpec.getId());
        pmSpecSku.setStatus("0");
        pmProdSpec.setSpecSkuList(pmProdSpecSkuService.getSpecSkuList(pmSpecSku));

        //库存模板
        PmProdSpecStockTemp prodSpecStockTemp = new PmProdSpecStockTemp();
        prodSpecStockTemp.setProdSpecId(pmProdSpec.getId());
        prodSpecStockTemp.setTempStatus(Constants.STATUS_VALID);
        pmProdSpec.setProdSpecStockTempList(getProdSpecStockTempList(prodSpecStockTemp));

        //属性信息
        PmProdSpecPrpty prodSpecPrpty = new PmProdSpecPrpty();
        prodSpecPrpty.setProdSpecId(pmProdSpec.getId());
        prodSpecPrpty.setStatus(Constants.STATUS_VALID);
        pmProdSpec.setProdSpecPrptyList(getProdSpecPrptyList(prodSpecPrpty));

        //详情信息
        pmProdSpec.setPmProdSpecDetail(getSpecDetailBySpecId(pmProdSpec.getId()));

        return pmProdSpec;
    }

    @Override
    public List<PmProdSpecDto> converDataList(List<PmProdSpec> prodSpecList) {
        if (prodSpecList != null && prodSpecList.size() > 0) {
            for (PmProdSpec pmProdSpec : prodSpecList) {
                converDataPic(pmProdSpec);
            }
        }
        List<PmProdSpecDto> dtoList = DozerUtils.mapList(mapper, prodSpecList, PmProdSpecDto.class);
        return dtoList;
    }

    @Override
    public PmProdSpecDto converDataInfo(PmProdSpec pmProdSpec) {
        converDataPic(pmProdSpec);
        PmProdSpecDto prodSpecDto = mapper.map(pmProdSpec, PmProdSpecDto.class);
        converDataSku(prodSpecDto);
        return prodSpecDto;
    }

    private void converDataPic(PmProdSpec pmProdSpec) {
        Map<String, String> picMap = new HashMap<String, String>();
        String picUri;
        if (pmProdSpec.getPicList() != null && pmProdSpec.getPicList().size() > 0) {
            for (PmProdSpecPic pic : pmProdSpec.getPicList()) {
                picUri = picMap.get(pic.getPicType());
                if (!StringUtils.isBlank(picUri)) {
                    picUri = picUri + ";" + pic.getPicId();
                } else {
                    picUri = pic.getPicId();
                }
                picMap.put(pic.getPicType(), picUri);
            }
        }
        /***业务代码*******************************************************************/
        String picMain = picMap.get(Constants.PROD_SPEC_PIC_TYPE_MAIN);
        if (!StringUtils.isBlank(picMain)) {
            pmProdSpec.setMainPicId(picMain);
        }
    }

    public void converDataSku(PmProdSpecDto prodSpecDto) {
        if (prodSpecDto.getSpecSkuList() != null && prodSpecDto.getSpecSkuList().size() > 0) {
            /**1.构造表头数据***********/
            JSONArray skuListLabel = new JSONArray();
            JSONObject labelObj = null;
            PmSpecSkuDto skuDto = prodSpecDto.getSpecSkuList().get(0);
            if (skuDto.getSpecPricePlanPrptyRelList() != null && skuDto.getSpecPricePlanPrptyRelList().size() > 0) {
                PmSpecPricePlanPrptyRel pricePlanPrptyRel = null;
                for (int i = 0; i < skuDto.getSpecPricePlanPrptyRelList().size(); i++) {
                    pricePlanPrptyRel = skuDto.getSpecPricePlanPrptyRelList().get(i);
                    labelObj = new JSONObject();
                    labelObj.put("label", pricePlanPrptyRel.getPrptyNameShow());
                    labelObj.put("prop", "prpty_" + pricePlanPrptyRel.getPrptyCode());
                    skuListLabel.add(labelObj);
                }
                labelObj = new JSONObject();
                labelObj.put("label", "价格");
                labelObj.put("prop", "price");
                skuListLabel.add(labelObj);
            }

            /**2.构造表数据***********/
            JSONArray skuTableData = new JSONArray();
            JSONObject dataObj = null;
            for (int j = 0; j < prodSpecDto.getSpecSkuList().size(); j++) {

                PmSpecSkuDto skuInfo = prodSpecDto.getSpecSkuList().get(j);
                if (skuInfo.getSpecPricePlanPrptyRelList() == null || skuInfo.getSpecPricePlanPrptyRelList().size() == 0) {
                    continue;
                }
                String mainId = "";

                dataObj = new JSONObject();
                dataObj.put("price", skuInfo.getPrice());

                PmSpecPricePlanPrptyRel pricePlanPrptyRel = null;
                for (int i = 0; i < skuInfo.getSpecPricePlanPrptyRelList().size(); i++) {
                    pricePlanPrptyRel = skuInfo.getSpecPricePlanPrptyRelList().get(i);
                    dataObj.put("prpty_" + pricePlanPrptyRel.getPrptyCode(), pricePlanPrptyRel.getPrptyValue());
                    dataObj.put("prptyNameShow_"+pricePlanPrptyRel.getPrptyCode(),pricePlanPrptyRel.getPrptyNameShow());

                    if (StringUtils.isBlank(mainId)) {
                        mainId = pricePlanPrptyRel.getPrptyCode() + "," + pricePlanPrptyRel.getPrptyValCode();
                    } else {
                        mainId = mainId + ";" + pricePlanPrptyRel.getPrptyCode() + "," + pricePlanPrptyRel.getPrptyValCode();
                    }
                }
                mainId = mainId + "|" + skuInfo.getId()+","+skuInfo.getCode();
                dataObj.put("mainId", mainId);
                dataObj.put("skuCode",skuInfo.getCode());
                skuTableData.add(dataObj);
            }
            prodSpecDto.setSkuListLabel(skuListLabel);
            prodSpecDto.setSkuTableData(skuTableData);
            prodSpecDto.setSpecSkuList(null);
        }
    }

    private void saveStockTemp(PmProdSpecSaveDto prodSpecDto) {
        //1 删除原有模板数据
        PmProdSpecStockTempExample example = new PmProdSpecStockTempExample();
        PmProdSpecStockTempExample.Criteria criteria = example.createCriteria();
        criteria.andProdSpecIdEqualTo(prodSpecDto.getId());
        pmProdSpecStockTempMapper.deleteByExample(example);
        if(prodSpecDto.getProdSpecStockTempList() !=null && prodSpecDto.getProdSpecStockTempList().size()>0){
            PmProdSpecStockTempDto specStockTempDto;
            PmProdSpecStockTemp specStockTemp;
            for(int i=0 ;i < prodSpecDto.getProdSpecStockTempList().size() ; i++){
               specStockTempDto = prodSpecDto.getProdSpecStockTempList().get(i);
               specStockTemp = mapper.map(specStockTempDto,PmProdSpecStockTemp.class);
               specStockTemp.setProdSpecId(prodSpecDto.getId());
               specStockTemp.setTempSeq(i);
               specStockTemp.setTempStatus(Constants.STATUS_VALID);
               pmProdSpecStockTempMapper.insert(specStockTemp);
            }
        }
    }

    private JSONObject getTestSpecStockTemp() {
        Common obj = new Common();
        obj.setCommonType(Constants.PROD_SPEC_TEST_STOCK_TEMP_TYPE);
        JSONObject resultObj = null;
        Common result = commonMapper.selectCommonObjSingle(obj);
        if (result != null && !StringUtils.isBlank(result.getCommonCode())) {
            resultObj = JSONObject.parseObject(result.getCommonCode());
        }
        return resultObj;
    }


    @Override
    public List<PmProdSpec> listPageApp(int pageNum, int pageSize, Map<String, String> searchMap) {
        PageHelper.startPage(pageNum, pageSize);
        List<PmProdSpec> prodSpecList = pmProdSpecMapper.searchService(searchMap);


        if (prodSpecList != null && prodSpecList.size() > 0) {
            Map<String, String> map;
            PmProdSkuInst pmProdSkuInst;
            PmProdSpecPic picParam;
            for (PmProdSpec prodSpec : prodSpecList) {

                //初始化服务对应商品最低价格
                map = new HashMap<>();
                map.put("prodSpecId", prodSpec.getId().toString());
                pmProdSkuInst = pmProdSkuInstMapper.getLowPrice(map);
                if (pmProdSkuInst != null && pmProdSkuInst.getPrice() > 0) {
                    prodSpec.setLowPrice(pmProdSkuInst.getPrice().toString());
                }
                //初始化图片信息
                picParam = new PmProdSpecPic();
                picParam.setProdSpecId(prodSpec.getId());
                prodSpec.setPicList(this.getPicList(picParam));
            }
        }
        //初始化图片信息
        return prodSpecList;
    }

    public List<PmPrpty> getSpecPrptyBySelective(String packageId, Long prodSpecId) {
        List<PmPrpty> prptyList = new ArrayList<PmPrpty>();
        if (!StringUtils.isBlank(packageId)) {
            String[] packageArray = packageId.split(",");
            if (packageArray != null && packageArray.length > 0) {
                for (String packageInfoId : packageArray) {
                    PmPrptyPackage prptyPackage = prptyPackageService.getById(Long.valueOf(packageInfoId));
                    if (prptyPackage != null && prptyPackage.getPrptyList() != null && prptyPackage.getPrptyList().size() > 0) {
                        for (PmPrpty prpty : prptyPackage.getPrptyList()) {
                            prptyList.add(prpty);
                        }
                    }
                }
            }
        }
        //过滤配有配置商品的属性及属性值
        return filterPrpty(prptyList, prodSpecId);
    }

    private List<PmPrpty> filterPrpty(List<PmPrpty> prptyList, Long prodSpecId) {
        List<PmPrpty> filterPrptyList = new ArrayList<PmPrpty>();
        List<PmPrptyVal> filterPrptyValList;
        Map<String, Object> paramMap;
        int count = 0;
        if (prptyList != null && prptyList.size() > 0) {
            for (PmPrpty prpty : prptyList) {
                filterPrptyValList = new ArrayList<PmPrptyVal>();
                if (prpty != null && prpty.getPrptyValList() != null && prpty.getPrptyValList().size() > 0) {
                    for (PmPrptyVal prptyVal : prpty.getPrptyValList()) {
                        paramMap = new HashMap<String, Object>();
                        paramMap.put("prodSpecId", prodSpecId);
                        paramMap.put("prptyCode", prptyVal.getPrptyCode());
                        paramMap.put("prptyValCode", prptyVal.getPrptyValCode());
                        if (Constants.PRPTY_TYPE_SALE.equals(prpty.getPrptyType())) {
                            count = pmProdPricePlanPrptyRelMapper.countProdPricePlanSelective(paramMap);
                        } else if (Constants.PRPTY_TYPE_NATURE.equals(prpty.getPrptyType())) {
                            count = pmProdPrptyValMapper.countProdPrptyValSelective(paramMap);
                        }
                        if (count > 0) {
                            filterPrptyValList.add(prptyVal);
                        }
                    }
                }
                if (filterPrptyValList.size() > 0) {
                    PmPrpty filterPrpty = mapper.map(prpty, PmPrpty.class);
                    filterPrpty.setPrptyValList(filterPrptyValList);
                    filterPrptyList.add(filterPrpty);
                }
            }
        }
        return filterPrptyList;
    }

    public List<Map<String, Object>> getSpecChannelBySelective(JSONArray salePrptyArray, JSONArray naturePrptyArray, String prodSpecType, Long prodSpecId) {
        StringBuffer sql = new StringBuffer();
        sql.append("select  ps.id as siteId,ps.dept_id as deptId,ps.dept_name as deptName,ps.site_name as siteName,ps.site_add as siteAdd,ps.business_hours as businessHours,ps.closing_time as closingTime,ps.responsible,ps.phone,ps.latitude ,ps.longitude,p.id as prodId  ");
        if (salePrptyArray != null && salePrptyArray.size() > 0) {
            sql.append(" ,pr0.sku_inst_id as skuInstId ");
        }
        sql.append(" from pm_product p,pm_prod_channel pc,pms_site ps,pm_prod_service_type pt");
        if (naturePrptyArray != null && naturePrptyArray.size() > 0) {
            for (int i = 0; i < naturePrptyArray.size(); i++) {
                sql.append(",pm_prod_prpty_val pv" + i);
            }
        }
        if (salePrptyArray != null && salePrptyArray.size() > 0) {
            for (int i = 0; i < salePrptyArray.size(); i++) {
                sql.append(",pm_prod_price_plan_prpty_rel pr" + i);
            }
        }
        sql.append(" where p.id=pc.prod_id and pc.prod_id and p.`status`=0 and pc.prod_spec_id=" + prodSpecId);
        sql.append(" and pc.dealership_id=ps.id and ps.`status`='01' and p.id=pt.prod_id and pt.service_type=1 ");
        if (naturePrptyArray != null && naturePrptyArray.size() > 0) {
            for (int i = 0; i < naturePrptyArray.size(); i++) {
                sql.append(" and pv" + i + ".prod_id=p.id and pv" + i + ".prpty_code='" + naturePrptyArray.getJSONObject(i).getString("prptyCode") + "' and pv" + i + ".prpty_val_code='" + naturePrptyArray.getJSONObject(i).getString("prptyValCode") + "'");
            }
        }
        if (salePrptyArray != null && salePrptyArray.size() > 0) {
            for (int i = 0; i < salePrptyArray.size(); i++) {
                sql.append(" and pr" + i + ".prod_id=p.id and pr" + i + ".prpty_code='" + salePrptyArray.getJSONObject(i).getString("prptyCode") + "' and pr" + i + ".prpty_val_code='" + salePrptyArray.getJSONObject(i).getString("prptyValCode") + "'");
            }
        }
        if (salePrptyArray != null && salePrptyArray.size() > 1) {
            for (int i = 1; i < salePrptyArray.size(); i++) {
                sql.append(" and pr" + i + ".sku_inst_id=pr0.sku_inst_id");
            }
        }
        log.debug("★★★★★★★★★★★★★★★★sqlcommon=" + sql.toString());
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("sql", sql.toString());
        List<Map<String, Object>> resultList = sqlCommonMapper.executeBySql(paramMap);
        return resultList;
        //        select  ps.id as siteId,ps.dept_id as deptId,ps.dept_name as deptName,ps.site_name as siteName,ps.site_add as siteAdd,
        //    ps.business_hours as businessHours,ps.closing_time as closingTime,ps.responsible,ps.phone,ps.latitude ,ps.longitude,
        //    p.id as prodId   ,pr0.sku_inst_id as skuInstId
        //     from pm_product p,pm_prod_channel pc,pms_site ps ,pm_prod_price_plan_prpty_rel pr0,pm_prod_price_plan_prpty_rel pr1
        //  where p.id=pc.prod_id and pc.prod_id and p.`status`=0 and pc.prod_spec_id=1
        //and pc.dealership_id=ps.id and ps.`status`='01'  and pr0.prod_id=p.id and pr0.prpty_code='1'
        //and pr0.prpty_val_code='1' and pr1.prod_id=p.id and pr1.prpty_code='2'
        //and pr1.prpty_val_code='5' and pr1.sku_inst_id=pr0.sku_inst_id
    }
    public List<Map<String, Object>> getSpecChannelByDoor(JSONArray salePrptyArray, JSONArray naturePrptyArray, String prodSpecType, Long prodSpecId,String provCode,String cityCode,String countyCode,String today,String endDay) {
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("sql", PmStockDoorSql.getStockCountSqlByDoor(salePrptyArray,naturePrptyArray,prodSpecType,prodSpecId,provCode,cityCode,countyCode,today,endDay));
        List<Map<String, Object>> resultList = sqlCommonMapper.executeBySql(paramMap);
        return resultList;
    }

    public void sortSpecChannel(List<Map<String, Object>> resultList, String latitude, String longitude) {
        if (resultList != null && resultList.size() > 0 && !StringUtils.isBlank(latitude) && !StringUtils.isBlank(longitude)) {
            String latitudeDB;
            String longitudeDB;
            Object latitudeObj;
            Object longitudObj;
            for (Map<String, Object> map : resultList) {
                latitudeDB = null;
                longitudeDB = null;
                latitudeObj = map.get("latitude");
                longitudObj = map.get("longitude");
                if (latitudeObj != null && longitudObj != null) {
                    latitudeDB = latitudeObj.toString();
                    longitudeDB = longitudObj.toString();
                }
                if (!StringUtils.isBlank(latitudeDB) && !StringUtils.isBlank(longitudeDB)) {
                    double distance = LocationUtils.getDistance(Double.valueOf(latitude).doubleValue(), Double.valueOf(longitude).doubleValue(),
                            Double.valueOf(latitudeDB).doubleValue(), Double.valueOf(longitudeDB).doubleValue());
                    map.put("distance", distance);
                } else {
                    map.put("distance", null);
                }
            }
            Collections.sort(resultList, new Comparator<Map<String, Object>>() {
                /*
                 * int compare(Object p1, Object p2) 返回一个基本类型的整型，
                 * 返回负数表示：p1 小于p2，
                 * 返回0 表示：p1和p2相等，
                 * 返回正数表示：p1大于p2
                 */
                public int compare(Map<String, Object> p1, Map<String, Object> p2) {
                    if (p1.get("distance") == null) {
                        return 1;
                    }
                    if (p2.get("distance") == null) {
                        return 1;
                    }
                    //按照Object的属性进行升序排列
                    if (Double.valueOf(p1.get("distance").toString()).doubleValue() > Double.valueOf(p2.get("distance").toString()).doubleValue()) {
                        return 1;
                    }
                    if (Double.valueOf(p1.get("distance").toString()).doubleValue() == Double.valueOf(p2.get("distance").toString()).doubleValue()) {
                        return 0;
                    }
                    return -1;
                }
            });
        }
    }

    public JSONObject getProdSpecSkuListShow(Long prodSpecId, Long packageId) {

        JSONObject retObj = new JSONObject();

        JSONArray skuListLabel = new JSONArray();
//        JSONArray skuTableData = new JSONArray();
        JSONArray skuTableDataNew = new JSONArray();
        List<PmPrpty> salePrptyList = new ArrayList<PmPrpty>();
        /**1.构造表头数据 ***********************/
        PmPrptyPackage prptyPackage = prptyPackageService.getById(packageId);
        if (prptyPackage != null && prptyPackage.getPrptyList() != null) {
            PmPrpty prpty = null;
            PmPrptyVal prptyVal = null;
            JSONObject labelObj = null;
            int index = -1;
            for (int i = 0; i < prptyPackage.getPrptyList().size(); i++) {
                prpty = prptyPackage.getPrptyList().get(i);
                if (Constants.PRPTY_TYPE_SALE.equals(prpty.getPrptyType())) {
                    index++;
                    labelObj = new JSONObject();
                    if (prpty != null && prpty.getPrptyValList() != null && prpty.getPrptyValList().size() > 0) {
                        salePrptyList.add(prpty);
                        labelObj = new JSONObject();
                        labelObj.put("label", prpty.getPrptyNameShow());
                        labelObj.put("prop", "prpty_" + index);
                        skuListLabel.add(labelObj);
                    }
                }
            }
            if (skuListLabel != null && skuListLabel.size() > 0) {
                labelObj = new JSONObject();
                labelObj.put("label", "价格");
                labelObj.put("prop", "price");
                skuListLabel.add(labelObj);
            }
        }
        //计算集合总数
        int count = 0;
        int number=salePrptyList.size();
        int total = 0;
        /**2.构造表数据 ***********************/

        List<List<PmPrptyVal>> prptyValList = new ArrayList<List<PmPrptyVal>>();
        for (int i = 0; i < number; i++) {
            prptyValList.add(salePrptyList.get(i).getPrptyValList());
        }
        List<List<String>> result = new ArrayList<List<String>>();
        descartes(prptyValList, result, 0, new ArrayList<String>());
        System.out.println("★★★★★★★★★★★★★★★ result="+result.toString());
        total = result.size();

        //初始化总数据
        JSONObject dataObjNew = null;
        for (int index = 0; index < total; index++) {
            dataObjNew = new JSONObject();
            dataObjNew.put("price",null);
            dataObjNew.put("mainId",null);
            skuTableDataNew.add(dataObjNew);
        }

        PmPrpty rsPrpty;
        PmPrptyVal rsPrptyVal;
        String mainIdNew;
        for(int rp_i =0; rp_i<salePrptyList.size(); rp_i++ ){
            rsPrpty =salePrptyList.get(rp_i);
            for(int rpv_j=0;rpv_j<rsPrpty.getPrptyValList().size();rpv_j++){
                rsPrptyVal = rsPrpty.getPrptyValList().get(rpv_j);
                for(int rs_j = 0 ;rs_j<total;rs_j++){
                    List<String> prptyListInfo =result.get(rs_j);
                    if(prptyListInfo.get(rp_i).equals(rsPrptyVal.getPrptyValCode())){
                        dataObjNew =skuTableDataNew.getJSONObject(rs_j);

                        dataObjNew.put("prpty_" + rp_i, rsPrptyVal.getPrptyValue());
                        dataObjNew.put("prptyNameShow_"+rp_i,rsPrpty.getPrptyNameShow());
                        mainIdNew = dataObjNew.getString("mainId");
                        if(StringUtils.isBlank(mainIdNew)){
                            mainIdNew = rsPrpty.getPrptyCode()+","+rsPrptyVal.getPrptyValCode();
                        }else{
                            mainIdNew = mainIdNew+";"+rsPrpty.getPrptyCode()+","+rsPrptyVal.getPrptyValCode();
                        }
                        dataObjNew.put("mainId", mainIdNew);
                    }
                }
            }
        }
//
//        if (salePrptyList != null && number > 0) {
//            for (int i = 0; i < number; i++) {
//                if (count == 0) {
//                    count = salePrptyList.get(i).getPrptyValList().size();
//                } else {
//                    count = count * salePrptyList.get(i).getPrptyValList().size();
//                }
//            }
//            //初始化总数据
//            JSONObject dataObj = null;
//            for (int index = 0; index < count; index++) {
//                dataObj = new JSONObject();
//                dataObj.put("price",null);
//                skuTableData.add(dataObj);
//            }
//
//            String prptyVal = "";
//            String prptyValCode = "";
//            String mainId="";
//            PmPrptyVal prptyValRet;
//
//            for (int i = 0; i < number; i++) {
//                for (int index = 0; index < count; index++) {
//                    dataObj = skuTableData.getJSONObject(index);
//                    prptyValRet = this.getPrptyVal(salePrptyList.get(i), index);
//                    if(prptyValRet!=null){
//                        prptyVal =prptyValRet.getPrptyValue();
//                        prptyValCode=prptyValRet.getPrptyValCode();
//                    }else{
//                        prptyVal ="";
//                        prptyValCode="";
//                    }
//                    dataObj.put("prpty_" + i, prptyVal);
//                    dataObj.put("prptyNameShow_"+i,salePrptyList.get(i).getPrptyNameShow());
//                    mainId = dataObj.getString("mainId");
//                    if(StringUtils.isBlank(mainId)){
//                        mainId = salePrptyList.get(i).getPrptyCode()+","+prptyValCode;
//                    }else{
//                        mainId = mainId+";"+salePrptyList.get(i).getPrptyCode()+","+prptyValCode;
//                    }
//                    dataObj.put("mainId", mainId);
//                }
//            }
//
//
//        }
        /**3.初始化数据库已存在数据 ***********************/
        if(prodSpecId!=null){
            PmSpecSku pmSpecSku = new PmSpecSku();
            pmSpecSku.setProdSpecId(prodSpecId);
            pmSpecSku.setStatus(String.valueOf(Constants.STATUS_VALID));
            List<PmSpecSku> pmSpecSkuList = pmProdSpecSkuService.getSpecSkuList(pmSpecSku);
            if(pmSpecSkuList!=null && pmSpecSkuList.size()>0){
                JSONObject dataObj = null;
                for(PmSpecSku sku : pmSpecSkuList){
                    if(sku.getSpecPricePlanPrptyRelList()==null || number != sku.getSpecPricePlanPrptyRelList().size()){
                        continue;
                    }
                    List<String> prptyValKeyList = new ArrayList<String>();
                    for(PmSpecPricePlanPrptyRel prptyRel : sku.getSpecPricePlanPrptyRelList()){
                        prptyValKeyList.add(prptyRel.getPrptyCode()+","+prptyRel.getPrptyValCode());
                    }
                    String[] dataObjKeyArray;
                    String mainId;
                    for (int index = 0; index < count; index++) {
                        dataObj=skuTableDataNew.getJSONObject(index);
                        mainId=dataObj.getString("mainId");
                        dataObjKeyArray =mainId.split(";");

                        boolean isFind=false;
                        if(dataObjKeyArray!=null && dataObjKeyArray.length>0 && prptyValKeyList!=null && prptyValKeyList.size()>0){
                            for(int m =0;m<dataObjKeyArray.length;m++){
                                String dataObjKey = dataObjKeyArray[m];
                                for(String prptyValKey : prptyValKeyList){
                                    if(dataObjKey.equals(prptyValKey)){
                                        isFind =true;
                                        break;
                                    }
                                }
                                if(isFind){
                                    if(m != dataObjKeyArray.length-1){
                                        isFind=false;
                                        continue;
                                    }
                                }else{
                                    break;
                                }
                            }
                            if(isFind){
                                dataObj.put("mainId",mainId+"|"+sku.getId()+","+sku.getCode());
                                dataObj.put("price",sku.getPrice());
                            }
                        }
                    }
                }
            }
        }

        /**4.返回************************************/
        retObj.put("skuListLabel",skuListLabel);
        retObj.put("skuTableData",skuTableDataNew);

        return retObj;
    }
    private void descartes(List<List<PmPrptyVal>> dimvalue, List<List<String>> result, int layer, List<String> curList) {
        if (layer < dimvalue.size() - 1) {
            if (dimvalue.get(layer).size() == 0) {
                descartes(dimvalue, result, layer + 1, curList);
            } else {
                for (int i = 0; i < dimvalue.get(layer).size(); i++) {
                    List<String> list = new ArrayList<String>(curList);
                    list.add(dimvalue.get(layer).get(i).getPrptyValCode());
                    descartes(dimvalue, result, layer + 1, list);
                }
            }
        } else if (layer == dimvalue.size() - 1) {
            if (dimvalue.get(layer).size() == 0) {
                result.add(curList);
            } else {
                for (int i = 0; i < dimvalue.get(layer).size(); i++) {
                    List<String> list = new ArrayList<String>(curList);
                    list.add(dimvalue.get(layer).get(i).getPrptyValCode());
                    result.add(list);
                }
            }
        }
    }
    private void descartesNew(List<List<PmPrptyVal>> dimvalue, List<List<JSONObject>> result, int layer, List<JSONObject> curList) {
        JSONObject temp = null;

        if (layer < dimvalue.size() - 1) {
            if (dimvalue.get(layer).size() == 0) {
                descartesNew(dimvalue, result, layer + 1, curList);
            } else {
                for (int i = 0; i < dimvalue.get(layer).size(); i++) {
                    List<JSONObject> list = new ArrayList<JSONObject>(curList);
                    temp = new JSONObject();
                    temp.put("prpty_"+i,dimvalue.get(layer).get(i).getPrptyValue());
                    list.add(temp);
                    temp = new JSONObject();
                    temp.put("prptyCode_"+i,dimvalue.get(layer).get(i).getPrptyCode());
                    list.add(temp);
                    descartesNew(dimvalue, result, layer + 1, list);
                }
            }
        } else if (layer == dimvalue.size() - 1) {
            if (dimvalue.get(layer).size() == 0) {
                result.add(curList);
            } else {
                for (int i = 0; i < dimvalue.get(layer).size(); i++) {
                    List<JSONObject> list = new ArrayList<JSONObject>(curList);
                    temp = new JSONObject();
                    temp.put("prpty_"+i,dimvalue.get(layer).get(i).getPrptyValue());
                    list.add(temp);
                    temp = new JSONObject();
                    temp.put("prptyCode_"+i,dimvalue.get(layer).get(i).getPrptyCode());
                    list.add(temp);
                    result.add(list);
                }
            }
        }
    }
    private PmPrptyVal getPrptyVal(PmPrpty prpty, int index){
        if(prpty!=null && prpty.getPrptyValList()!=null){
            int size=prpty.getPrptyValList().size();
//            int remainder =index%size;
            int multiple =index%size;
            return prpty.getPrptyValList().get(multiple);
        }
        return null;
    }

    public PmProdSpecDetail getSpecDetailBySpecId(Long prodSpecId){
        PmProdSpecDetailExample detailExample = new PmProdSpecDetailExample();
        PmProdSpecDetailExample.Criteria detailCriteria = detailExample.createCriteria();
        detailCriteria.andProdSpecIdEqualTo(prodSpecId);

        List<PmProdSpecDetail> specDetailList = pmProdSpecDetailMapper.selectByExampleWithBLOBs(detailExample);

        if(specDetailList !=null && specDetailList.size()>0){
            return specDetailList.get(0);
        }else{
            return new PmProdSpecDetail();
        }
    }
    public PmProdSpecDetailDto getSpecDetailDtoBySpecId(Long prodSpecId ){

        PmProdSpecDetail detail = this.getSpecDetailBySpecId(prodSpecId);
        PmProdSpecDetailDto detailDto  = mapper.map(detail, PmProdSpecDetailDto.class);

        return detailDto;
    }

    public AppProdSpecDto getByIdApp(Long id){
        PmProdSpec pmProdSpec = pmProdSpecMapper.selectByPrimaryKey(id);
        PmServerSpec  pmServerSpec = pmServerSpecMapper.selectByPrimaryKey(pmProdSpec.getServerSpecId());
            if(pmServerSpec!=null){
            pmProdSpec.setServerSpecName(pmServerSpec.getServerSpecName());
        }
        //图片信息
        PmProdSpecPic pic = new PmProdSpecPic();
        pic.setProdSpecId(pmProdSpec.getId());
        pmProdSpec.setPicList(getPicList(pic));
        this.converDataPic(pmProdSpec);

        //详情信息
        pmProdSpec.setPmProdSpecDetail(getSpecDetailBySpecId(pmProdSpec.getId()));

        AppProdSpecDto prodSpecAppDto = mapper.map(pmProdSpec, AppProdSpecDto.class);

        Map<String,Object> searchMap = new HashMap<String,Object>();
        searchMap.put("prodSpecId",prodSpecAppDto.getId());

        //在售服务属性
        prodSpecAppDto.setPrptyList(buildSpecPrptyApp(searchMap));
        //在售SKU
        prodSpecAppDto.setSkuList(buildSpecSkuApp(searchMap));

        // 上门服务、到店服务
        HashMap map = new HashMap<>();
        map.put("prodSpecId", id.toString());
        List<Map<String,Object>> serverModeList = pmProdSpecMapper.searchServiceMode(map);
        if(serverModeList!=null && serverModeList.size()>0){
            String serverModeId ="";
            String serverModeName="";
            for(Map<String,Object> mapDB : serverModeList){
                if(!StringUtils.isBlank(serverModeId)){
                    serverModeId = serverModeId +","+mapDB.get("service_type");
                    serverModeName =serverModeName+","+mapDB.get("service_type_name");
                }else{
                    serverModeId = ""+mapDB.get("service_type");
                    serverModeName =""+mapDB.get("service_type_name");
                }
            }
            prodSpecAppDto.setServerModeId(serverModeId);
            prodSpecAppDto.setServerModeName(serverModeName);
        }

        return  prodSpecAppDto;
    }

    private  List<AppPrptyDto> buildSpecPrptyApp(Map<String,Object> searchMap){

        List<Map<String,Object>> specPrptyMapList=  pmProdSpecMapper.searchSpecPrptyAppByProdSpecId(searchMap);
        List<AppPrptyDto> ptptyDtoAppList = new ArrayList<AppPrptyDto>();
        if(specPrptyMapList!=null && specPrptyMapList.size()>0){
            Map<String,String> prptyMap= new HashMap<String,String>();
            Object prptyCodeObj;
            for(Map<String,Object> dbMap : specPrptyMapList){
                prptyCodeObj = dbMap.get("prpty_code") ;
                if(prptyCodeObj !=null ){
                    prptyMap.put(prptyCodeObj.toString(),"");
                }
            }
            AppPrptyDto ptptyApp;

            List<AppPrptyValDto> ptptyValDtoAppList;
            AppPrptyValDto prptyValApp=null;
            for(String key : prptyMap.keySet()) {
                ptptyApp = null;
                ptptyValDtoAppList = null;
                for (Map<String, Object> dbMap : specPrptyMapList) {
                    if (key.equals(dbMap.get("prpty_code").toString())) {
                        if (ptptyApp == null) {
                            ptptyApp = new AppPrptyDto();
                            ptptyApp.setPrptyCode(key);
                            ptptyApp.setPrptyNameShow(dbMap.get("prpty_name_show").toString());
                            ptptyApp.setPrptyType(dbMap.get("prpty_type").toString());
                            ptptyValDtoAppList = new ArrayList<AppPrptyValDto>();
                        }

                        prptyValApp = new AppPrptyValDto();
                        prptyValApp.setPrptyValCode(dbMap.get("prpty_val_code").toString());
                        prptyValApp.setPrptyValue(dbMap.get("prpty_value").toString());
                        ptptyValDtoAppList.add(prptyValApp);
                    }
                }
                if (ptptyApp != null) {
                    ptptyApp.setPrptyValList(ptptyValDtoAppList);
                    ptptyDtoAppList.add(ptptyApp);
                }
            }
        }
        return ptptyDtoAppList;
    }
    private  List<AppProdSpecSkuDto> buildSpecSkuApp(Map<String,Object> searchMap){

        List<Map<String,Object>> specSkuMapList=  pmProdSpecMapper.searchSpecSkuAppByProdSpecId(searchMap);
        List<AppProdSpecSkuDto> skuDtoAppList = new ArrayList<AppProdSpecSkuDto>();
        if(specSkuMapList!=null && specSkuMapList.size()>0){
            Map<String,String> skuMap= new HashMap<String,String>();
            Object skuCodeObj;
            for(Map<String,Object> dbMap : specSkuMapList){
                skuCodeObj = dbMap.get("sku_code") ;
                if(skuCodeObj !=null ){
                    skuMap.put(skuCodeObj.toString(),"");
                }
            }
            AppProdSpecSkuDto skuApp;

            List<AppProdSpecSkuDetailDto> skuDtailDtoAppList;
            AppProdSpecSkuDetailDto skuDtailApp=null;
            for(String key : skuMap.keySet()) {
                skuApp = null;
                skuDtailDtoAppList = null;
                for (Map<String, Object> dbMap : specSkuMapList) {
                    if (key.equals(dbMap.get("sku_code").toString())) {
                        if (skuApp == null) {
                            skuApp = new AppProdSpecSkuDto();
                            skuApp.setSkuCode(key);
                            skuApp.setPrice(Float.valueOf(dbMap.get("price").toString()));
                            skuDtailDtoAppList = new ArrayList<AppProdSpecSkuDetailDto>();
                        }

                        skuDtailApp = new AppProdSpecSkuDetailDto();
                        skuDtailApp.setPrptyCode(dbMap.get("prpty_code").toString());
                        skuDtailApp.setPrptyNameShow(dbMap.get("prpty_name_show").toString());
                        skuDtailApp.setPrptyValCode(dbMap.get("prpty_val_code").toString());
                        skuDtailApp.setPrptyValue(dbMap.get("prpty_value").toString());
                        skuDtailDtoAppList.add(skuDtailApp);
                    }
                }
                if (skuApp != null) {
                    skuApp.setSkuDetailList(skuDtailDtoAppList);
                    skuDtoAppList.add(skuApp);
                }
            }
        }
        return skuDtoAppList;
    }
}